<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chuangges.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="人脸识别、文字识别、语音识别、语音合成">
<meta property="og:type" content="article">
<meta property="og:title" content="百度智能云之在线识别">
<meta property="og:url" content="https://chuangges.gitee.io/2023/01/10/19-%E7%99%BE%E5%BA%A6%E8%AF%86%E5%88%AB/index.html">
<meta property="og:site_name" content="闯哥要去流浪了">
<meta property="og:description" content="人脸识别、文字识别、语音识别、语音合成">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chuangges.gitee.io/images/web/audio-view.png">
<meta property="article:published_time" content="2023-01-10T03:17:56.000Z">
<meta property="article:modified_time" content="2023-04-14T08:19:59.305Z">
<meta property="article:author" content="chuangges">
<meta property="article:tag" content="前端技术栈">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chuangges.gitee.io/images/web/audio-view.png">

<link rel="canonical" href="https://chuangges.gitee.io/2023/01/10/19-%E7%99%BE%E5%BA%A6%E8%AF%86%E5%88%AB/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>百度智能云之在线识别 | 闯哥要去流浪了</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="闯哥要去流浪了" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">闯哥要去流浪了</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">愿你岁月可回首，且以深情共白头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chuangges.gitee.io/2023/01/10/19-%E7%99%BE%E5%BA%A6%E8%AF%86%E5%88%AB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chuang.png">
      <meta itemprop="name" content="chuangges">
      <meta itemprop="description" content="chuangge's blog.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闯哥要去流浪了">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          百度智能云之在线识别
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-01-10 11:17:56" itemprop="dateCreated datePublished" datetime="2023-01-10T11:17:56+08:00">2023-01-10</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-04-14 16:19:59" itemprop="dateModified" datetime="2023-04-14T16:19:59+08:00">2023-04-14</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">前端技术栈</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>31k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>28 分钟</span>
            </span>
            <div class="post-description">人脸识别、文字识别、语音识别、语音合成</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="一、准备工作"><a href="#一、准备工作" class="headerlink" title="一、准备工作"></a>一、准备工作</h1><ul>
<li><strong>Android、IOS 证书</strong><ul>
<li>Android 平台签名证书：打包发布 apk 时用来签名的数字证书（.keystore)。</li>
<li>IOS 开发证书：用于 APP 测试时安装到苹果手机真机的运行情况。</li>
<li>IOS 发布证书：用于 APP 上线时打包 ipa 并上传到 App Store。</li>
</ul>
</li>
<li><strong>百度智能云</strong><ul>
<li>获取密钥：<a target="_blank" rel="external nofollow noopener noreferrer" href="https://ai.baidu.com/">百度AI开放平台</a>、注册&#x2F;登录、实名认证、人脸识别、创建应用。</li>
<li>领取额度：<a target="_blank" rel="external nofollow noopener noreferrer" href="https://console.bce.baidu.com/ai/?_=1668753171812#/ai/face/overview/index">人脸识别</a>、<a target="_blank" rel="external nofollow noopener noreferrer" href="https://console.bce.baidu.com/ai/?_=1668753171812#/ai/speech/overview/index">文字识别</a>、<a target="_blank" rel="external nofollow noopener noreferrer" href="https://console.bce.baidu.com/ai/?_=1668753171812#/ai/ocr/overview/index">语音识别</a>。</li>
<li>查看文档：<a target="_blank" rel="external nofollow noopener noreferrer" href="https://ai.baidu.com/ai-doc/FACE/yk37c1u4t">人脸识别</a>、<a target="_blank" rel="external nofollow noopener noreferrer" href="https://cloud.baidu.com/doc/OCR/s/Ek3h7xypm">文字识别</a>、<a target="_blank" rel="external nofollow noopener noreferrer" href="https://ai.baidu.com/ai-doc/SPEECH/Ek39uxgre">语音识别</a>。</li>
</ul>
</li>
<li><strong>引用插件和申请权限</strong><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 插件库：人脸检测库 tracking、脸部追踪 face</span></span><br><span class="line">&lt;script src=<span class="string">&quot;js/tracking/tracking.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;js/tracking/face.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 系统权限：联网、查看网络状态、录像、录音、音量修改、拍照</span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.INTERNET&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.ACCESS_NETWORK_STATE&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.RECORD_VIDEO&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.RECORD_AUDIO&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.MODIFY_AUDIO_SETTINGS&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.CAMERA&quot;</span> /&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 应用权限：每次安装后都需要重新设置</span></span><br><span class="line">智能工厂的权限设置、设置、应用和通知、权限管理器、允许麦克风/相机权限</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="二、接口封装"><a href="#二、接口封装" class="headerlink" title="二、接口封装"></a>二、接口封装</h1>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="variable constant_">APP_ID</span> = <span class="string">&quot;28095556&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="variable constant_">API_KEY</span> = <span class="string">&quot;Tdi9l1a4jl7U0V1DsFz7AWpj&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="variable constant_">SECRET_KEY</span> = <span class="string">&quot;v3I7gbR6PGxB978k8RsHzNQvyB2o7gbQ&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> baiduAipSdk = <span class="built_in">require</span>(<span class="string">&quot;baidu-aip-sdk&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> <span class="title class_">FaceClient</span> = <span class="keyword">new</span> baiduAipSdk.<span class="title function_">face</span>(<span class="variable constant_">APP_ID</span>, <span class="variable constant_">API_KEY</span>, <span class="variable constant_">SECRET_KEY</span>);</span><br><span class="line"><span class="keyword">let</span> <span class="title class_">OcrClient</span> = <span class="keyword">new</span> baiduAipSdk.<span class="title function_">ocr</span>(<span class="variable constant_">APP_ID</span>, <span class="variable constant_">API_KEY</span>, <span class="variable constant_">SECRET_KEY</span>);</span><br><span class="line"><span class="keyword">let</span> <span class="title class_">SpeechClient</span> = <span class="keyword">new</span> baiduAipSdk.<span class="title function_">speech</span>(<span class="variable constant_">APP_ID</span>, <span class="variable constant_">API_KEY</span>, <span class="variable constant_">SECRET_KEY</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 人脸检测</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> <span class="variable">method</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> <span class="variable">image</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> imageType：BASE64/URL/FACE_TOKEN，默认 base64</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> userId：最多注册 20 个人脸</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> groupId：对应多个 user_id 的用户组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> groupIdList：从指定的 group 查找，用逗号分隔 (上限十个)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> faceToken：人脸图片的唯一标识</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  请求体格式化：Content-Type: application/json</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  Base64 图片：去掉头部 data:image/png;base64,</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  图片格式：支持 png/jpg/bmp/jpeg，不支持 gif</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  每个 appid 对应一个人脸库，每个用户可以多次注册一个人脸</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="built_in">exports</span>.<span class="property">AipFaceClient</span> = <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">prames</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> newErr = <span class="keyword">new</span> <span class="title class_">Error</span>();</span><br><span class="line">        newErr.<span class="property">leval</span> = <span class="number">9</span>;</span><br><span class="line">        newErr.<span class="property">title</span> = <span class="string">&quot;参数错误&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> result</span><br><span class="line">    <span class="keyword">let</span> method = prames.<span class="property">method</span> || <span class="string">&#x27;search&#x27;</span></span><br><span class="line">    <span class="keyword">let</span> imageType = prames.<span class="property">imageType</span> || <span class="string">&#x27;BASE64&#x27;</span></span><br><span class="line">    <span class="keyword">if</span> (method != <span class="string">&#x27;faceDelete&#x27;</span> &amp;&amp; !prames.<span class="property">image</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> = <span class="string">&#x27;请传入 image 参数&#x27;</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ([<span class="string">&#x27;addUser&#x27;</span>, <span class="string">&#x27;updateUser&#x27;</span>, <span class="string">&#x27;faceDelete&#x27;</span>].<span class="title function_">includes</span>(method) &amp;&amp; !prames.<span class="property">user</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> = <span class="string">&#x27;请传入 user 参数&#x27;</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> user = prames.<span class="property">user</span></span><br><span class="line">    <span class="keyword">let</span> dept = prames.<span class="property">Department</span> || user.<span class="property">Department</span></span><br><span class="line">    <span class="keyword">let</span> image = prames.<span class="property">image</span>.<span class="title function_">replace</span>(<span class="string">&#x27;data:image/png;base64,&#x27;</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    <span class="keyword">switch</span> (method) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;addUser&#x27;</span>:    <span class="comment">// 注册：每个用户组可注册多次</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">FaceClient</span>.<span class="title function_">addUser</span>(image, imageType, dept, user.<span class="property">_id</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;updateUser&#x27;</span>: <span class="comment">// 更新</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">FaceClient</span>.<span class="title function_">updateUser</span>(image, imageType, dept, user.<span class="property">_id</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;faceDelete&#x27;</span>: <span class="comment">// 删除</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">FaceClient</span>.<span class="title function_">faceDelete</span>(user.<span class="property">_id</span>, dept, prames.<span class="property">faceToken</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;detect&#x27;</span>:     <span class="comment">// 检测</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">FaceClient</span>.<span class="title function_">detect</span>(image, imageType)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;search&#x27;</span>:</span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">FaceClient</span>.<span class="title function_">search</span>(image, imageType, prames.<span class="property">groupIdList</span>)</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (result.<span class="property">error_msg</span> &amp;&amp; result.<span class="property">error_msg</span> != <span class="string">&#x27;SUCCESS&#x27;</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> = result.<span class="property">error_msg</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 文字识别</span></span><br><span class="line"><span class="built_in">exports</span>.<span class="property">AipOcrClient</span> = <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">prames</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> newErr = <span class="keyword">new</span> <span class="title class_">Error</span>();</span><br><span class="line">        newErr.<span class="property">leval</span> = <span class="number">9</span>;</span><br><span class="line">        newErr.<span class="property">title</span> = <span class="string">&quot;参数错误&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> result, method = prames.<span class="property">method</span> || <span class="string">&#x27;accurateBasic&#x27;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (!prames.<span class="property">image</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> = <span class="string">&#x27;请传入 image 参数&#x27;</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> image = prames.<span class="property">image</span>.<span class="title function_">replace</span>(<span class="string">&#x27;data:image/png;base64,&#x27;</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    <span class="keyword">switch</span> (method) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;accurateBasic&#x27;</span>:   <span class="comment">// 通用文字识别（高精度版）</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">OcrClient</span>.<span class="title function_">accurateBasic</span>(image, prames.<span class="property">options</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;licensePlate&#x27;</span>:    <span class="comment">// 车牌识别</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">OcrClient</span>.<span class="title function_">licensePlate</span>(image, prames.<span class="property">options</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;idcard&#x27;</span>:          <span class="comment">// 身份证</span></span><br><span class="line">            <span class="keyword">let</span> idCardSide = prames.<span class="property">idCardSide</span> || <span class="string">&#x27;front&#x27;</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">OcrClient</span>.<span class="title function_">idcard</span>(image, idCardSide, prames.<span class="property">options</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;docAnalysis&#x27;</span>:     <span class="comment">// 办公文档</span></span><br><span class="line">            <span class="keyword">let</span> lType = prames.<span class="property">languageType</span> || <span class="string">&#x27;CHN_ENG&#x27;</span></span><br><span class="line">            <span class="keyword">let</span> rType = prames.<span class="property">resultType</span> || <span class="string">&#x27;big&#x27;</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">OcrClient</span>.<span class="title function_">docAnalysis</span>(image, lType, rType, prames.<span class="property">options</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (result.<span class="property">error_msg</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> = result.<span class="property">error_msg</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@title</span> 语音识别：语音转文字 </span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> method：请求方法 recognize</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> speech：包含语音内容的 Buffer 对象，默认单声道、8000 采样率、wav 格式。</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  声道数：声音的通道的数目，单声道使用一个喇叭，立体声两个喇叭分左右声道。</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  采样频率：取样频率，指每秒钟取得声音样本的次数，频率越高则声音质量越好。</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  采样位数：将采样幅度量化的取样值，是用来衡量声音波动变化的分辨率，取值 8/16。</span></span><br><span class="line"><span class="comment">  *                8 位指将数据分为 2 的 8 次方即 256 份，16 则是 16 次方。</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> format：音频格式</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  pcm：不压缩不需要解码的原始格式，根据采样频率和位数对量化数据进行编码。</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  wav：不压缩的无损格式，只是在 PCM 数据前加上 44 字节来描述采样率、声道数等。</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  amr：压缩比大于 mp3 的自适应多速率编码格式，由于质量较差而常用于语音通话。</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> rate：采样频率，取值 8000 / 16000</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> options：主要包含 用户唯一标识 cuid、语言类型 dev_pid。</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@title</span> 语音合成：文字转语音</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> method：请求方法 text2audio</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> text：UTF-8 编码文本、长度小于 1024 字节</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@prame</span> text2audio options：spd 语速 0-9、pit 音调 0-9、vol 音量 0-15、per 发音人</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * <span class="doctag">@title</span> 数据转换</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  base64 编码：Buffer.from(&#x27;hello world&#x27;, &#x27;utf8&#x27;).toString(&#x27;base64&#x27;)</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desp</span>  base64 解码：Buffer.from(&#x27;aGVsbG8gd29ybGQ=&#x27;, &#x27;base64&#x27;)</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="built_in">exports</span>.<span class="property">AipSpeechClient</span> = <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">prames</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> newErr = <span class="keyword">new</span> <span class="title class_">Error</span>();</span><br><span class="line">        newErr.<span class="property">leval</span> = <span class="number">9</span>;</span><br><span class="line">        newErr.<span class="property">title</span> = <span class="string">&quot;参数错误&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> result;</span><br><span class="line">    <span class="keyword">switch</span> (prames.<span class="property">method</span>) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;recognize&#x27;</span>:</span><br><span class="line">            <span class="keyword">if</span> (!prames.<span class="property">speech</span>) &#123;</span><br><span class="line">                newErr.<span class="property">message</span> = <span class="string">&#x27;请传入参数 speech&#x27;</span></span><br><span class="line">                <span class="keyword">throw</span> newErr</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">let</span> speech = prames.<span class="property">speech</span>.<span class="title function_">replace</span>(<span class="string">&#x27;data:audio/wav;base64,&#x27;</span></span><br><span class="line">            <span class="keyword">let</span> buffer = <span class="title class_">Buffer</span>.<span class="title function_">from</span>(speech, <span class="string">&#x27;&#x27;</span>), <span class="string">&#x27;base64&#x27;</span>)</span><br><span class="line">            <span class="keyword">let</span> format = prames.<span class="property">format</span> || <span class="string">&#x27;wav&#x27;</span></span><br><span class="line">            <span class="keyword">let</span> rate = prames.<span class="property">rate</span> || <span class="number">8000</span></span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">SpeechClient</span>.<span class="title function_">recognize</span>(buffer, format, rate, prames.<span class="property">options</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;text2audio&#x27;</span>:</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">if</span> (!prames.<span class="property">text</span>) &#123;</span><br><span class="line">                newErr.<span class="property">message</span> = <span class="string">&#x27;请传入 text 参数&#x27;</span></span><br><span class="line">                <span class="keyword">throw</span> newErr</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">let</span> options = prames.<span class="property">options</span> || &#123; <span class="attr">spd</span>: <span class="number">5</span>, <span class="attr">pit</span>: <span class="number">5</span>, <span class="attr">per</span>: <span class="number">0</span> &#125;</span><br><span class="line">            result = <span class="keyword">await</span> <span class="title class_">SpeechClient</span>.<span class="title function_">text2audio</span>(prames.<span class="property">text</span>, options)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    newErr.<span class="property">message</span> = result.<span class="property">error_msg</span> || result.<span class="property">err_msg</span></span><br><span class="line">    <span class="keyword">if</span> (newErr.<span class="property">message</span>.<span class="title function_">indexOf</span>(<span class="string">&#x27;limit reached&#x27;</span>) &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">        newErr.<span class="property">message</span> =  <span class="string">&#x27;没有当前接口额度&#x27;</span></span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (newErr.<span class="property">message</span> &amp;&amp; newErr.<span class="property">message</span> != <span class="string">&#x27;success.&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">throw</span> newErr</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (prames.<span class="property">method</span> == <span class="string">&#x27;recognize&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> result.<span class="property">result</span>[<span class="number">0</span>]</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;data:audio/wav;base64,&#x27;</span> + <span class="title class_">Buffer</span>.<span class="title function_">from</span>(result.<span class="property">data</span>).<span class="title function_">toString</span>(<span class="string">&#x27;base64&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="三、H5-识别"><a href="#三、H5-识别" class="headerlink" title="三、H5 识别"></a>三、H5 识别</h1><blockquote>
<p>人脸识别通过 WebRTC getUserMedia 实时获取视频流数据，然后通过第三方插件 face-api.js&#x2F;tracking.js 识别人脸，注意移动浏览器的 WebRTC 兼容方案和必须使用 https 网址。</p>
</blockquote>
<h2 id="图片上传"><a href="#图片上传" class="headerlink" title="图片上传"></a>图片上传</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">.<span class="property">input</span>-file-control &#123;</span><br><span class="line">    <span class="attr">display</span>: flex;</span><br><span class="line">    <span class="attr">position</span>: relative;</span><br><span class="line">    margin-<span class="attr">right</span>: 15px;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">input</span>-file-control input &#123;</span><br><span class="line">    <span class="attr">opacity</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attr">position</span>: absolute;</span><br><span class="line">    <span class="attr">left</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attr">right</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attr">top</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attr">bottom</span>: <span class="number">0</span>;</span><br><span class="line">    z-<span class="attr">index</span>: <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> onchange：报错 function 未定义，内容改变并且失焦后触发</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> <span class="variable">ng</span>-change：必须配合 ng-model 使用，只要内容改变就会触发</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> angular.element(this).scope().fn：自定义 onchange 事件</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;input-file-control&quot;</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;primary&quot;</span>&gt;</span>图片上传<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span> <span class="attr">accept</span>=<span class="string">&quot;image/*&quot;</span> <span class="attr">capture</span>=<span class="string">&quot;camera&quot;</span> </span></span></span><br><span class="line"><span class="tag"><span class="language-xml">    <span class="attr">onchange</span>=<span class="string">&quot;angular.element(this).scope().changeImg(this.files[0])&quot;</span> /&gt;</span></span></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">$scope.<span class="property">changeImg</span> = <span class="keyword">function</span> (<span class="params">file</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!file) <span class="keyword">return</span>;</span><br><span class="line">    <span class="title function_">fileReader</span>(file).<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">baseImg</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">111</span>, baseImg)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fileReader</span> (file, method) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="keyword">function</span> (<span class="params">resolve, reject</span>) &#123;</span><br><span class="line">        method = method || <span class="string">&#x27;readAsDataURL&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> reader = <span class="keyword">new</span> <span class="title class_">FileReader</span>();</span><br><span class="line">        reader[method](file);</span><br><span class="line">        reader.<span class="property">onloadend</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title function_">resolve</span>(reader.<span class="property">result</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        reader.<span class="property">onerror</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title function_">reject</span>(<span class="string">&#x27;图片读取失败&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">compress</span> (baseImg) &#123;</span><br><span class="line">    <span class="keyword">let</span> img = <span class="keyword">new</span> <span class="title class_">Image</span>();</span><br><span class="line">    img.<span class="property">src</span> = baseImg;</span><br><span class="line">    img.<span class="title function_">setAttribute</span>(<span class="string">&quot;crossOrigin&quot;</span>, <span class="string">&quot;Anonymous&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> </span><br><span class="line">        (img.<span class="property">onload</span> = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> canvas = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;canvas&quot;</span>),</span><br><span class="line">                ctx = canvas.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>),</span><br><span class="line">                w = img.<span class="property">width</span>,</span><br><span class="line">                h = img.<span class="property">height</span>;</span><br><span class="line">            canvas.<span class="property">width</span> = w;</span><br><span class="line">            canvas.<span class="property">height</span> = h;</span><br><span class="line">            ctx.<span class="title function_">drawImage</span>(img, <span class="number">0</span>, <span class="number">0</span>, w, h);</span><br><span class="line">            <span class="keyword">let</span> baseImg = canvas.<span class="title function_">toDataURL</span>(<span class="string">&quot;image/png&quot;</span>, <span class="number">0.6</span>)</span><br><span class="line">            <span class="title function_">resolve</span>(baseImg);</span><br><span class="line">        &#125;)</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="人脸拍照"><a href="#人脸拍照" class="headerlink" title="人脸拍照"></a>人脸拍照</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line">$scope.<span class="property">onShowFace</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    imanDialog.<span class="title function_">showFace</span>().<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">base64Img</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">111</span>, base64Img)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">showFace</span> () &#123;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">this</span></span><br><span class="line">    <span class="keyword">var</span> defer = $q.<span class="title function_">defer</span>();</span><br><span class="line">    <span class="keyword">var</span> scope = $rootScope.$new(<span class="literal">true</span>);</span><br><span class="line">    scope.<span class="property">scanTip</span> = <span class="string">&#x27;正在调取摄像头&#x27;</span></span><br><span class="line">    scope.<span class="property">video</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">canvas</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">trackerTask</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">tracker</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">context</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">stream</span> = <span class="literal">null</span></span><br><span class="line">    scope.<span class="property">isTokePhoto</span> = <span class="literal">false</span></span><br><span class="line">    scope.<span class="property">base64Img</span> = <span class="literal">null</span></span><br><span class="line">    <span class="keyword">var</span> templateString = <span class="string">&#x27;&lt;ion-modal-view class=&quot;im-modal im-modal--face&quot;&gt;&#x27;</span> +</span><br><span class="line">        <span class="string">&#x27;&lt;div class=&quot;scanTip&quot;&gt;&#123;&#123;scanTip&#125;&#125;&lt;/div&gt;&#x27;</span> +</span><br><span class="line">        <span class="string">&#x27;&lt;video ng-if=&quot;!base64Img&quot; id=&quot;imVideo&quot; width=&quot;400&quot; height=&quot;300&quot; preload &#x27;</span> + </span><br><span class="line">        <span class="string">&#x27;autoplay loop muted&gt;&lt;/video&gt;&#x27;</span> +</span><br><span class="line">        <span class="string">&#x27;&lt;canvas ng-if=&quot;!base64Img&quot; id=&quot;imCanvas&quot; width=&quot;400&quot; height=&quot;300&quot;&gt;&lt;/canvas&gt;&#x27;</span> +</span><br><span class="line">        <span class="string">&#x27;&lt;img ng-if=&quot;base64Img&quot; src=&quot;&#123;&#123;base64Img&#125;&#125;&quot; width=&quot;400&quot; height=&quot;300&quot; /&gt;&#x27;</span> +</span><br><span class="line">    <span class="string">&#x27;&lt;/ion-modal-view&gt;&#x27;</span></span><br><span class="line">    <span class="keyword">var</span> modal = $ionicModal.<span class="title function_">fromTemplate</span>(templateString, &#123;</span><br><span class="line">        <span class="attr">scope</span>: scope,</span><br><span class="line">        <span class="attr">animation</span>: <span class="string">&#x27;im-slide-in-right&#x27;</span></span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    scope.$on(<span class="string">&#x27;modal.shown&#x27;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        navigator.<span class="property">mediaDevices</span>.<span class="title function_">getUserMedia</span>(&#123;</span><br><span class="line">            <span class="attr">video</span>: &#123; <span class="attr">width</span>: <span class="number">400</span>, <span class="attr">height</span>: <span class="number">300</span>, <span class="attr">facingMode</span>: <span class="string">&quot;user&quot;</span> &#125;</span><br><span class="line">        &#125;, <span class="keyword">function</span> (<span class="params">stream</span>) &#123;</span><br><span class="line">            scope.<span class="property">stream</span> = stream</span><br><span class="line">            scope.<span class="property">video</span> = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;imVideo&#x27;</span>)</span><br><span class="line">            scope.<span class="property">canvas</span> = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;imCanvas&#x27;</span>)</span><br><span class="line">            scope.<span class="property">context</span> = scope.<span class="property">canvas</span>.<span class="title function_">getContext</span>(<span class="string">&#x27;2d&#x27;</span>, &#123;<span class="attr">willReadFrequently</span>: <span class="literal">true</span>&#125;)</span><br><span class="line">            <span class="keyword">if</span> (<span class="string">&quot;srcObject&quot;</span> <span class="keyword">in</span> scope.<span class="property">video</span>) &#123;</span><br><span class="line">                scope.<span class="property">video</span>.<span class="property">srcObject</span> = stream</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                scope.<span class="property">video</span>.<span class="property">src</span> = <span class="variable language_">window</span>.<span class="property">URL</span>.<span class="title function_">createObjectURL</span>(stream)</span><br><span class="line">            &#125;</span><br><span class="line">            scope.<span class="property">video</span>.<span class="property">onloadedmetadata</span> = <span class="keyword">function</span> (<span class="params">e</span>) &#123; scope.<span class="property">video</span>.<span class="title function_">play</span>() &#125;</span><br><span class="line">            scope.<span class="property">tracker</span> = <span class="keyword">new</span> tracking.<span class="title class_">ObjectTracker</span>(<span class="string">&#x27;face&#x27;</span>)</span><br><span class="line">            scope.<span class="property">tracker</span>.<span class="title function_">setInitialScale</span>(<span class="number">4</span>)</span><br><span class="line">            scope.<span class="property">tracker</span>.<span class="title function_">setStepSize</span>(<span class="number">2</span>)</span><br><span class="line">            scope.<span class="property">tracker</span>.<span class="title function_">setEdgesDensity</span>(<span class="number">0.1</span>)</span><br><span class="line">            scope.<span class="property">trackerTask</span> = tracking.<span class="title function_">track</span>(<span class="string">&#x27;#imVideo&#x27;</span>, scope.<span class="property">tracker</span>, &#123;<span class="attr">camera</span>: <span class="literal">true</span>&#125;)</span><br><span class="line">            scope.<span class="property">tracker</span>.<span class="title function_">on</span>(<span class="string">&#x27;track&#x27;</span>, handleTracked)</span><br><span class="line"></span><br><span class="line">        &#125;, <span class="keyword">function</span> (<span class="params">error</span>) &#123;</span><br><span class="line">            _this.<span class="title function_">toastBottom</span>(<span class="string">&#x27;访问用户媒体失败：&#x27;</span> + error.<span class="property">message</span>)</span><br><span class="line">            modal.<span class="title function_">hide</span>();</span><br><span class="line">            defer.<span class="title function_">reject</span>();</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">handleTracked</span> (event) &#123;</span><br><span class="line">        <span class="keyword">if</span> (event.<span class="property">data</span>.<span class="property">length</span> == <span class="number">0</span> &amp;&amp; !scope.<span class="property">isTokePhoto</span>) &#123;</span><br><span class="line">            scope.<span class="property">scanTip</span> = <span class="string">&#x27;未识别到人脸&#x27;</span></span><br><span class="line">            scope.$apply()</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line">        scope.<span class="property">context</span>.<span class="title function_">clearRect</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">400</span>, <span class="number">300</span>)</span><br><span class="line">        event.<span class="property">data</span>.<span class="title function_">forEach</span>(<span class="keyword">function</span>(<span class="params">rect</span>) &#123;</span><br><span class="line">            scope.<span class="property">context</span>.<span class="property">strokeStyle</span> = <span class="string">&#x27;#eb652e&#x27;</span></span><br><span class="line">            scope.<span class="property">context</span>.<span class="title function_">strokeRect</span>(rect.<span class="property">x</span>, rect.<span class="property">y</span>, rect.<span class="property">width</span>, rect.<span class="property">height</span>)</span><br><span class="line">            scope.<span class="property">context</span>.<span class="property">font</span> = <span class="string">&#x27;11px Helvetica&#x27;</span></span><br><span class="line">            scope.<span class="property">context</span>.<span class="property">fillStyle</span> = <span class="string">&#x27;#fff&#x27;</span></span><br><span class="line">            <span class="keyword">let</span> rectWidth = rect.<span class="property">x</span> + rect.<span class="property">width</span> + <span class="number">5</span></span><br><span class="line">            scope.<span class="property">context</span>.<span class="title function_">fillText</span>(<span class="string">&#x27;x: &#x27;</span> + rect.<span class="property">x</span> + <span class="string">&#x27;px&#x27;</span>, rectWidth, rect.<span class="property">y</span> + <span class="number">11</span>)</span><br><span class="line">            scope.<span class="property">context</span>.<span class="title function_">fillText</span>(<span class="string">&#x27;y: &#x27;</span> + rect.<span class="property">y</span> + <span class="string">&#x27;px&#x27;</span>, rectWidth, rect.<span class="property">y</span> + <span class="number">22</span>)</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">if</span> (!scope.<span class="property">isTokePhoto</span>) &#123;</span><br><span class="line">            scope.<span class="property">isTokePhoto</span> = <span class="literal">true</span></span><br><span class="line">            scope.<span class="property">scanTip</span> = <span class="string">&#x27;正在拍照，请保持当前姿势 2 秒&#x27;</span></span><br><span class="line">            scope.$apply()</span><br><span class="line">            <span class="keyword">var</span> timer = <span class="built_in">setTimeout</span>(<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">                <span class="built_in">clearTimeout</span>(timer)</span><br><span class="line">                scope.<span class="property">context</span>.<span class="title function_">drawImage</span>(scope.<span class="property">video</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">400</span>, <span class="number">300</span>)</span><br><span class="line">                scope.<span class="property">base64Img</span> = scope.<span class="property">canvas</span>.<span class="title function_">toDataURL</span>(<span class="string">&#x27;image/png&#x27;</span>, <span class="number">0.6</span>)</span><br><span class="line">                modal.<span class="title function_">hide</span>()</span><br><span class="line">                defer.<span class="title function_">resolve</span>(scope.<span class="property">base64Img</span>);</span><br><span class="line">            &#125;, <span class="number">2000</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    scope.$on(<span class="string">&#x27;modal.hidden&#x27;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        scope.<span class="property">context</span> &amp;&amp; scope.<span class="property">context</span>.<span class="title function_">clearRect</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">400</span>, <span class="number">300</span>)</span><br><span class="line">        scope.<span class="property">tracker</span> &amp;&amp; scope.<span class="property">tracker</span>.<span class="title function_">removeListener</span>(<span class="string">&#x27;track&#x27;</span>, handleTracked)</span><br><span class="line">        scope.<span class="property">trackerTask</span> &amp;&amp; scope.<span class="property">trackerTask</span>.<span class="title function_">stop</span>()  <span class="comment">// 停止侦测</span></span><br><span class="line">        <span class="keyword">if</span> (scope.<span class="property">stream</span>) &#123;</span><br><span class="line">            scope.<span class="property">stream</span>.<span class="property">enabled</span> = <span class="literal">false</span></span><br><span class="line">            scope.<span class="property">stream</span>.<span class="title function_">getTracks</span>()[<span class="number">0</span>].<span class="title function_">stop</span>()</span><br><span class="line">            scope.<span class="property">stream</span>.<span class="title function_">getVideoTracks</span>()[<span class="number">0</span>].<span class="title function_">stop</span>()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (scope.<span class="property">video</span>.<span class="property">srcObject</span>) &#123;</span><br><span class="line">            scope.<span class="property">video</span>.<span class="property">srcObject</span>.<span class="title function_">getTracks</span>()[<span class="number">0</span>].<span class="title function_">stop</span>()</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="variable language_">window</span>.<span class="property">URL</span>.<span class="title function_">revokeObjectURL</span>(scope.<span class="property">video</span>.<span class="property">src</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        $timeout(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            modal.<span class="property">scope</span>.$destroy();</span><br><span class="line">            modal.<span class="property">$el</span>.<span class="title function_">remove</span>();</span><br><span class="line">        &#125;, modal.<span class="property">hideDelay</span> || <span class="number">320</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    modal.<span class="title function_">show</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> defer.<span class="property">promise</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="语音录制"><a href="#语音录制" class="headerlink" title="语音录制"></a>语音录制</h2>  <div align="center">
    <img src="" data-original="/images/web/audio-view.png" alt="浏览器结构" width="50%">
  </div>

  <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-toast-view</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">z-index</span>: <span class="number">10</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-radius</span>: <span class="number">4px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-size</span>: <span class="number">14px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">left</span>: <span class="number">50%</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">top</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">max-width</span>: <span class="number">95%</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">transform</span>: <span class="built_in">translateX</span>(-<span class="number">50%</span>);</span></span><br><span class="line"><span class="language-css">        -webkit-<span class="attribute">transform</span>: <span class="built_in">translateX</span>(-<span class="number">50%</span>);</span></span><br><span class="line"><span class="language-css">        <span class="attribute">padding</span>: <span class="number">30px</span> <span class="number">15px</span> <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background</span>: <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.6</span>);</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-toast-view</span> <span class="selector-class">.im-voice-wrap</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-bottom</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">justify-content</span>: center;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-toast-view</span> <span class="selector-tag">i</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">font-size</span>: <span class="number">54px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-left</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">2px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-top</span>: <span class="number">5px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite7 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">2</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">26px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite6 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">3</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">22px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite5 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">4</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">18px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite4 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">5</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">14px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite3 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">6</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: backgroundInfinite2 <span class="number">2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.voice-animation</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-of-type</span>(<span class="number">7</span>) &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">6px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite2 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">10%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">15%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite3 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">25%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">30%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite4 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">40%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">45%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite5 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">55%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">60%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite6 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">70%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">75%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> backgroundInfinite7 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">85%</span> &#123; <span class="attribute">background</span>: transparent; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">90%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">background</span>: <span class="number">#fff</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-audio--item</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">align-items</span>: center;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-bottom</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-audio-view</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">align-items</span>: center;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background</span>: <span class="number">#d5e5ff</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">padding</span>: <span class="number">4px</span> <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-radius</span>: <span class="number">4px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.im-item-view--text</span> <span class="selector-class">.label</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-right</span>: <span class="number">18px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-style</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">align-items</span>: center;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-radius</span>: <span class="number">4px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-right</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-style</span> <span class="selector-tag">span</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border</span>: <span class="number">2px</span> solid <span class="number">#4c4c4c</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-top-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-left-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-bottom-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-radius</span>: <span class="number">50%</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">box-sizing</span>: border-box;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-style</span> <span class="selector-class">.small</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border</span>: <span class="number">4px</span> solid <span class="number">#4c4c4c</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-top-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-left-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-bottom-color</span>: transparent;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-style</span> <span class="selector-class">.middle</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">16px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">16px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-left</span>: -<span class="number">11px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">opacity</span>: <span class="number">1</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-style</span> <span class="selector-class">.large</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">24px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">24px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">margin-left</span>: -<span class="number">19px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">opacity</span>: <span class="number">1</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-playing</span> <span class="selector-class">.middle</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: audioPlaying <span class="number">1.2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.audio-playing</span> <span class="selector-class">.large</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">animation</span>: audioPlaying2 <span class="number">1.2s</span> ease-in-out infinite;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> audioPlaying &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">opacity</span>: <span class="number">0</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">10%</span> &#123; <span class="attribute">opacity</span>: <span class="number">1</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">opacity</span>: <span class="number">0</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="keyword">@keyframes</span> audioPlaying2 &#123;</span></span><br><span class="line"><span class="language-css">        <span class="number">0%</span> &#123; <span class="attribute">opacity</span>: <span class="number">0</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">50%</span> &#123; <span class="attribute">opacity</span>: <span class="number">1</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">60%</span> &#123; <span class="attribute">opacity</span>: <span class="number">0</span>; &#125;</span></span><br><span class="line"><span class="language-css">        <span class="number">100%</span> &#123; <span class="attribute">opacity</span>: <span class="number">0</span>; &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">im-card</span> <span class="attr">linenumber</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;im-audio--item&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;label&quot;</span>&gt;</span>录制语音：<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;im-audio-view&quot;</span> <span class="attr">ng-click</span>=<span class="string">&quot;playAudio()&quot;</span> <span class="attr">ng-if</span>=<span class="string">&quot;audio&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;audio-style&quot;</span> </span></span><br><span class="line"><span class="tag">                <span class="attr">ng-class</span>=<span class="string">&quot;&#123;&#x27;audio-playing&#x27;: audioStatus == 2&#125;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;small&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;middle&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;large&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;seconds&quot;</span>&gt;</span>&#123;&#123;duration&#125;&#125;&quot;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">im-item-view</span> </span></span><br><span class="line"><span class="tag">        <span class="attr">class</span>=<span class="string">&quot;im-item-view--text&quot;</span> </span></span><br><span class="line"><span class="tag">        <span class="attr">name</span>=<span class="string">&quot;识别结果：&quot;</span> </span></span><br><span class="line"><span class="tag">        <span class="attr">value</span>=<span class="string">&quot;speechData.audioText&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">im-item-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">im-card</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;im-toast-view&quot;</span> <span class="attr">ng-if</span>=<span class="string">&quot;audioStatus == 1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;im-voice-wrap&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;ion-ios-mic&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;voice-animation&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>手指上滑，取消录制<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;primary ion-ios-mic-outline&quot;</span> <span class="attr">on-hold</span>=<span class="string">&quot;onStartAudio()&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">on-release</span>=<span class="string">&quot;onStopAudio()&quot;</span> <span class="attr">on-swipe-up</span>=<span class="string">&quot;onStopAudio(true)&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>&#123;&#123;audioStatus == 1 ? &#x27;录音中...&#x27; : &#x27;按住录音&#x27;&#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> createScript：存储用于语音识别的 PCM 音频、语音时长</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> MediaRecorder：存储用于网页回放的 audio/webm 音频</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> wav 格式：一般是在 pcm 文件前增加 44 个字节的文件头</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> createScript：存储单声道 PCM 数据</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line">$scope.<span class="property">audioStatus</span> = <span class="number">0</span>    <span class="comment">// 0 初始、1 正在录制、2 正在播放</span></span><br><span class="line">$scope.<span class="property">audioCtx</span> = <span class="keyword">new</span> <span class="title class_">AudioContext</span>();</span><br><span class="line"></span><br><span class="line">$scope.<span class="property">onStartAudio</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!navigator.<span class="property">mediaDevices</span>) &#123; </span><br><span class="line">        imanDialog.<span class="title function_">toastBottom</span>(<span class="string">&#x27;当前浏览器不支持麦克风&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> </span><br><span class="line">    &#125;</span><br><span class="line">    $scope.<span class="property">audio</span> = <span class="literal">null</span></span><br><span class="line">    $scope.<span class="property">mediaRecorder</span> = <span class="literal">null</span></span><br><span class="line">    $scope.<span class="property">duration</span> = <span class="number">0</span></span><br><span class="line">    $scope.<span class="property">bufferSize</span> = <span class="number">0</span></span><br><span class="line">    $scope.<span class="property">bufferArray</span> = []</span><br><span class="line">    navigator.<span class="property">mediaDevices</span>.<span class="title function_">getUserMedia</span>(&#123;<span class="attr">audio</span>: <span class="literal">true</span>&#125;)</span><br><span class="line">    .<span class="title function_">then</span>(<span class="function"><span class="params">stream</span> =&gt;</span> &#123;</span><br><span class="line">        $scope.<span class="property">stream</span> = stream</span><br><span class="line">        $scope.<span class="property">audioStatus</span> = <span class="number">1</span></span><br><span class="line">        $scope.<span class="property">audioInput</span> = $scope.<span class="property">audioCtx</span>.<span class="title function_">createMediaStreamSource</span>(stream)</span><br><span class="line">        <span class="keyword">let</span> createScript = $scope.<span class="property">audioCtx</span>.<span class="property">createScriptProcessor</span> || </span><br><span class="line">                           $scope.<span class="property">audioCtx</span>.<span class="property">createJavaScriptNode</span>;</span><br><span class="line">        <span class="comment">// 采集 4096 就触发一次 onaudioprocess、声道数</span></span><br><span class="line">        $scope.<span class="property">recorder</span> = createScript.<span class="title function_">apply</span>($scope.<span class="property">audioCtx</span>, [<span class="number">4096</span>, <span class="number">1</span>]);  </span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 开始录音</span></span><br><span class="line">        $scope.<span class="property">audioInput</span>.<span class="title function_">connect</span>($scope.<span class="property">recorder</span>);</span><br><span class="line">        $scope.<span class="property">recorder</span>.<span class="title function_">connect</span>($scope.<span class="property">audioCtx</span>.<span class="property">destination</span>)</span><br><span class="line">        $scope.<span class="property">recorder</span>.<span class="property">onaudioprocess</span> = <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span><br><span class="line">            <span class="comment">// 返回一个 Float32Array 的 PCM 数据</span></span><br><span class="line">            <span class="keyword">let</span> channelData = e.<span class="property">inputBuffer</span>.<span class="title function_">getChannelData</span>(<span class="number">0</span>) </span><br><span class="line">            $scope.<span class="property">bufferArray</span>.<span class="title function_">push</span>(channelData.<span class="title function_">slice</span>(<span class="number">0</span>));</span><br><span class="line">            $scope.<span class="property">bufferSize</span> += channelData.<span class="property">length</span></span><br><span class="line">        &#125;; </span><br><span class="line">        <span class="keyword">let</span> chunks = []</span><br><span class="line">        $scope.<span class="property">mediaRecorder</span> = <span class="keyword">new</span> <span class="title class_">MediaRecorder</span>(stream);</span><br><span class="line">        $scope.<span class="property">mediaRecorder</span>.<span class="title function_">start</span>();</span><br><span class="line">        $scope.<span class="property">mediaRecorder</span>.<span class="property">ondataavailable</span> = <span class="function">(<span class="params">e</span>) =&gt;</span>&#123;</span><br><span class="line">            chunks.<span class="title function_">push</span>(e.<span class="property">data</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        $scope.<span class="property">mediaRecorder</span>.<span class="property">onstop</span> = <span class="function">() =&gt;</span>&#123;</span><br><span class="line">            <span class="keyword">let</span> blob = <span class="keyword">new</span> <span class="title class_">Blob</span>(chunks, &#123;<span class="attr">type</span>:<span class="string">&quot;audio/webm;codecs=opus&quot;</span>&#125;); </span><br><span class="line">            <span class="keyword">let</span> audioUrl = (<span class="variable language_">window</span>.<span class="property">URL</span> || webkitURL).<span class="title function_">createObjectURL</span>(blob);</span><br><span class="line">            $scope.<span class="property">audio</span> = <span class="keyword">new</span> <span class="title class_">Audio</span>(audioUrl)</span><br><span class="line">        &#125;</span><br><span class="line">        $scope.$apply()</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">catch</span>(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">        imanDialog.<span class="title function_">toastBottom</span>((err &amp;&amp; err.<span class="property">message</span> || err))</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$scope.<span class="property">onStopAudio</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!$scope.<span class="property">recorder</span>) &#123; <span class="keyword">return</span> &#125;</span><br><span class="line">    $scope.<span class="property">recorder</span>.<span class="title function_">disconnect</span>();</span><br><span class="line">    $scope.<span class="property">audioInput</span>.<span class="title function_">disconnect</span>();</span><br><span class="line">    $scope.<span class="property">stream</span>.<span class="title function_">getTracks</span>()[<span class="number">0</span>].<span class="title function_">stop</span>();</span><br><span class="line">    $scope.<span class="property">duration</span> = <span class="title function_">getDuration</span>();</span><br><span class="line">    $scope.<span class="property">audioStatus</span> = <span class="number">0</span></span><br><span class="line">    $scope.<span class="property">mediaRecorder</span>.<span class="title function_">stop</span>()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$scope.<span class="property">playAudio</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 播放：createBufferSource 方式、audio 方式</span></span><br><span class="line">    <span class="keyword">if</span> ($scope.<span class="property">audioStatus</span> == <span class="number">2</span>) &#123;</span><br><span class="line">        $scope.<span class="property">audioStatus</span> = <span class="number">0</span></span><br><span class="line">        $scope.<span class="property">audio</span>.<span class="title function_">pause</span>()</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    $scope.<span class="property">audioStatus</span> = <span class="number">2</span></span><br><span class="line">    $scope.<span class="property">audio</span>.<span class="title function_">play</span>();</span><br><span class="line">    $scope.<span class="property">audio</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;ended&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        $scope.<span class="property">audioStatus</span> = <span class="number">0</span></span><br><span class="line">        $scope.$apply()</span><br><span class="line">    &#125;, <span class="literal">false</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getDuration</span> () &#123;</span><br><span class="line">    <span class="keyword">let</span> duration = (<span class="number">4096</span>*$scope.<span class="property">bufferArray</span>.<span class="property">length</span>) / $scope.<span class="property">audioCtx</span>.<span class="property">sampleRate</span></span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">RoundNum</span>(duration, &#123; <span class="title class_">RoundType</span>: <span class="number">3</span>, <span class="title class_">RoundValue</span>: <span class="number">5</span>, <span class="title class_">Precision</span>: <span class="number">2</span> &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">RoundNum</span> (val, round) &#123;</span><br><span class="line">    <span class="keyword">var</span> returnValue;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (val == <span class="literal">undefined</span> || val == <span class="literal">null</span> || val.<span class="title function_">toString</span>().<span class="title function_">trim</span>() == <span class="string">&#x27;&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (round &amp;&amp; angular.<span class="title function_">isObject</span>(round)) &#123;</span><br><span class="line">        round.<span class="property">Precision</span> = round.<span class="property">Precision</span> || <span class="number">0</span>;   <span class="comment">// 精度</span></span><br><span class="line">        round.<span class="property">RoundType</span> = round.<span class="property">RoundType</span> || <span class="number">3</span>;   <span class="comment">// 舍入类型</span></span><br><span class="line">        round.<span class="property">RoundValue</span> = round.<span class="property">RoundValue</span> || <span class="number">5</span>; <span class="comment">// 舍入值</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (round.<span class="property">RoundType</span> == <span class="number">1</span>) &#123;               <span class="comment">// 1 全部进位</span></span><br><span class="line">            returnValue = <span class="built_in">parseInt</span>(val * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span> + <span class="number">1</span>));</span><br><span class="line">            <span class="keyword">if</span> (returnValue % <span class="number">10</span> &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                returnValue = <span class="built_in">parseInt</span>(val * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>)) + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                returnValue = <span class="built_in">parseInt</span>(val * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            returnValue = returnValue / <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>);</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (round.<span class="property">RoundType</span> == <span class="number">2</span>) &#123;       <span class="comment">// 2 全部舍位</span></span><br><span class="line">            <span class="keyword">var</span> _Precision = <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>)</span><br><span class="line">            returnValue = <span class="built_in">parseInt</span>(val * _Precision) / _Precision;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;                                 <span class="comment">// 3 按值舍入</span></span><br><span class="line">            returnValue = <span class="built_in">parseInt</span>(val * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span> + <span class="number">1</span>));</span><br><span class="line">            <span class="keyword">if</span> (returnValue % <span class="number">10</span> &gt;= round.<span class="property">RoundValue</span>) &#123;</span><br><span class="line">                returnValue = <span class="built_in">parseInt</span>(val * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>)) + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                returnValue = <span class="built_in">parseInt</span>(returnValue / <span class="number">10</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            returnValue = returnValue / <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, round.<span class="property">Precision</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">parseFloat</span>(returnValue).<span class="title function_">toFixed</span>(round.<span class="property">Precision</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="语音处理"><a href="#语音处理" class="headerlink" title="语音处理"></a>语音处理</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line">$scope.<span class="property">onSpeechRecognize</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// speech 语音对象：单声道、8000 采样率、wav 格式</span></span><br><span class="line">    <span class="keyword">let</span> bufferData = <span class="title function_">mergeBuffers</span>($scope.<span class="property">bufferArray</span>, $scope.<span class="property">bufferSize</span>);</span><br><span class="line">    <span class="keyword">let</span> bufferWav = <span class="title function_">encodeToWAV</span>(bufferData)</span><br><span class="line">    <span class="title class_">DataService</span>.<span class="title function_">fileReader</span>(bufferWav).<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">baseAudio</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">AipSpeechClient</span>(&#123; <span class="attr">method</span>: <span class="string">&#x27;recognize&#x27;</span>, <span class="attr">speech</span>: baseAudio &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">text</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(text)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 数据合并压缩：将多个 Float32Array 数组合并为一个 Float32Array</span></span><br><span class="line"><span class="comment"> * 根据采样率压缩，比如输入 48k 输出 16k，则输入数据中每隔 3 取 1 位</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">float32array</span>&#125; data        [-1, 1]的 pcm 数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; inputSampleRate   输入采样率</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125; outputSampleRate  输出采样率</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span>  &#123;<span class="type">float32array</span>&#125;          压缩处理后的二进制数据</span></span><br><span class="line"><span class="comment">**/</span>     </span><br><span class="line"><span class="keyword">function</span> <span class="title function_">mergeBuffers</span> (bufferList, length, inputSampleRate, outputSampleRate) &#123;</span><br><span class="line">    <span class="comment">// 默认</span></span><br><span class="line">    inputSampleRate = inputSampleRate || (<span class="keyword">new</span> <span class="title class_">AudioContext</span>().<span class="property">sampleRate</span>)</span><br><span class="line">    outputSampleRate = outputSampleRate || <span class="number">8000</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 合并</span></span><br><span class="line">    <span class="keyword">var</span> offset = <span class="number">0</span></span><br><span class="line">    <span class="keyword">var</span> data = <span class="keyword">new</span> <span class="title class_">Float32Array</span>(length);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; bufferList.<span class="property">length</span>; i += <span class="number">1</span>) &#123;</span><br><span class="line">        data.<span class="title function_">set</span>(bufferList[i], offset);</span><br><span class="line">        offset += bufferList[i].<span class="property">length</span>;</span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="comment">// 压缩</span></span><br><span class="line">    <span class="keyword">var</span> compression = <span class="built_in">parseInt</span> (inputSampleRate / outputSampleRate);</span><br><span class="line">    <span class="keyword">var</span> outLength = <span class="title class_">Math</span>.<span class="title function_">ceil</span>(data.<span class="property">length</span> / compression);</span><br><span class="line">    <span class="keyword">var</span> result = <span class="keyword">new</span> <span class="title class_">Float32Array</span>(outLength);</span><br><span class="line">    <span class="keyword">var</span> s = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; outLength; i += <span class="number">1</span>) &#123;</span><br><span class="line">        result[i] = data[<span class="title class_">Math</span>.<span class="title function_">floor</span>(s)];</span><br><span class="line">        s += compression;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * wav 格式一般是在 pcm 文件前增加 44 个字节的文件头</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">DataView</span>&#125; bytes           pcm 二进制数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125;  SampleRate       输入采样率，输出为 8000</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &#123;<span class="type">number</span>&#125;  sampleBits       输出采样位数，取值 8、16</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns</span> &#123;<span class="type">DataView</span>&#125;               wav 二进制数据</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">encodeToWAV</span> (bytes, sampleRate, sampleBits=<span class="number">16</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> dataLength = bytes.<span class="property">length</span> * (sampleBits / <span class="number">8</span>);</span><br><span class="line">    <span class="keyword">var</span> buffer = <span class="keyword">new</span> <span class="title class_">ArrayBuffer</span>(<span class="number">44</span> + dataLength);</span><br><span class="line">    <span class="keyword">var</span> view = <span class="keyword">new</span> <span class="title class_">DataView</span>(buffer);</span><br><span class="line">    <span class="keyword">var</span> channelCount = <span class="number">1</span>;  <span class="comment">// 百度只支持单声道</span></span><br><span class="line">    <span class="keyword">var</span> offset = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> writeString = <span class="keyword">function</span> (<span class="params">str</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str.<span class="property">length</span>; i += <span class="number">1</span>) &#123;</span><br><span class="line">            view.<span class="title function_">setUint8</span>(offset + i, str.<span class="title function_">charCodeAt</span>(i));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// write PCM</span></span><br><span class="line">    <span class="comment">// 资源交换文件标识符</span></span><br><span class="line">    <span class="title function_">writeString</span>(<span class="string">&#x27;RIFF&#x27;</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 下个地址开始到文件尾总字节数,即文件大小-8</span></span><br><span class="line">    view.<span class="title function_">setUint32</span>(offset, <span class="number">36</span> + dataLength, <span class="literal">true</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// WAV文件标志</span></span><br><span class="line">    <span class="title function_">writeString</span>(<span class="string">&#x27;WAVE&#x27;</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 波形格式标志</span></span><br><span class="line">    <span class="title function_">writeString</span>(<span class="string">&#x27;fmt &#x27;</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 过滤字节,一般为 0x10 = 16</span></span><br><span class="line">    view.<span class="title function_">setUint32</span>(offset, <span class="number">16</span>, <span class="literal">true</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 格式类别 (PCM形式采样数据)</span></span><br><span class="line">    view.<span class="title function_">setUint16</span>(offset, <span class="number">1</span>, <span class="literal">true</span>); offset += <span class="number">2</span>;</span><br><span class="line">    <span class="comment">// 通道数</span></span><br><span class="line">    view.<span class="title function_">setUint16</span>(offset, channelCount, <span class="literal">true</span>); offset += <span class="number">2</span>;</span><br><span class="line">    <span class="comment">// 采样率，每秒样本数，表示每个通道的播放速度</span></span><br><span class="line">    view.<span class="title function_">setUint32</span>(offset, sampleRate, <span class="literal">true</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 波形数据传输率 (每秒平均字节数) 单声道 × 每秒数据位数 × 每样本数据位 / 8</span></span><br><span class="line">    view.<span class="title function_">setUint32</span>(offset, channelCount * sampleRate * (sampleBits / <span class="number">8</span>), <span class="literal">true</span>); </span><br><span class="line">    offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 快数据调整数 采样一次占用字节数 单声道 × 每样本的数据位数 / 8</span></span><br><span class="line">    view.<span class="title function_">setUint16</span>(offset, channelCount * (sampleBits / <span class="number">8</span>), <span class="literal">true</span>); offset += <span class="number">2</span>;</span><br><span class="line">    <span class="comment">// 每样本数据位数</span></span><br><span class="line">    view.<span class="title function_">setUint16</span>(offset, sampleBits, <span class="literal">true</span>); offset += <span class="number">2</span>;</span><br><span class="line">    <span class="comment">// 数据标识符</span></span><br><span class="line">    <span class="title function_">writeString</span>(<span class="string">&#x27;data&#x27;</span>); offset += <span class="number">4</span>;</span><br><span class="line">    <span class="comment">// 采样数据总数，即数据总大小-44</span></span><br><span class="line">    view.<span class="title function_">setUint32</span>(offset, dataLength, <span class="literal">true</span>); offset += <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 写入采样数据</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; bytes.<span class="property">length</span>; i += <span class="number">1</span>, offset += sampleBits / <span class="number">8</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> s = <span class="title class_">Math</span>.<span class="title function_">max</span>(-<span class="number">1</span>, <span class="title class_">Math</span>.<span class="title function_">min</span>(<span class="number">1</span>, bytes[i]));</span><br><span class="line">        <span class="keyword">var</span> val = s &lt; <span class="number">0</span> ? s * <span class="number">0x8000</span> : s * <span class="number">0x7fff</span>;</span><br><span class="line">        <span class="keyword">if</span> (sampleBits == <span class="number">8</span>) &#123;</span><br><span class="line">            view.<span class="title function_">setInt8</span>(offset, <span class="built_in">parseInt</span>(<span class="number">255</span> / (<span class="number">65535</span>/(val+<span class="number">32768</span>)), <span class="number">10</span>), <span class="literal">true</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            view.<span class="title function_">setInt16</span>(offset, val, <span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Blob</span>([view], &#123; <span class="attr">type</span>: <span class="string">&#x27;audio/wav&#x27;</span> &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">encodeToPCM</span> (bytes, sampleBits=<span class="number">16</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> dataLength = bytes.<span class="property">length</span> * (sampleBits / <span class="number">8</span>);</span><br><span class="line">    <span class="keyword">var</span> buffer = <span class="keyword">new</span> <span class="title class_">ArrayBuffer</span>(dataLength);</span><br><span class="line">    <span class="keyword">var</span> view = <span class="keyword">new</span> <span class="title class_">DataView</span>(buffer);</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i=<span class="number">0</span>, offset=<span class="number">0</span>; i &lt; bytes.<span class="property">length</span>; i += <span class="number">1</span>, offset += sampleBits/<span class="number">8</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> s = <span class="title class_">Math</span>.<span class="title function_">max</span>(-<span class="number">1</span>, <span class="title class_">Math</span>.<span class="title function_">min</span>(<span class="number">1</span>, bytes[i]));</span><br><span class="line">        <span class="keyword">var</span> val = s &lt; <span class="number">0</span> ? s * <span class="number">0x8000</span> : s * <span class="number">0x7fff</span>;</span><br><span class="line">        <span class="keyword">if</span> (sampleBits === <span class="number">8</span>) &#123;</span><br><span class="line">            view.<span class="title function_">setInt8</span>(offset, <span class="built_in">parseInt</span>(<span class="number">255</span> / (<span class="number">65535</span>/(val+<span class="number">32768</span>)), <span class="number">10</span>), <span class="literal">true</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            view.<span class="title function_">setInt16</span>(offset, val, <span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">return</span> view.<span class="property">buffer</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="四、微信识别"><a href="#四、微信识别" class="headerlink" title="四、微信识别"></a>四、微信识别</h1><blockquote>
<p>微信浏览器无法调用 getUserMedia，而且必须使用微信的人脸识别而不能使用第三方插件。</p>
</blockquote>
<h2 id="拍照上传"><a href="#拍照上传" class="headerlink" title="拍照上传"></a>拍照上传</h2>  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">// 小程序</span><br><span class="line"><span class="tag">&lt;<span class="name">van-image</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span> <span class="attr">round</span> <span class="attr">fit</span>=<span class="string">&quot;cover&quot;</span> <span class="attr">:src</span>=<span class="string">&quot;imgSrc&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">van-button</span> @<span class="attr">click</span>=<span class="string">&quot;toCam(&#x27;camera&#x27;)&quot;</span>&gt;</span>拍照认证<span class="tag">&lt;/<span class="name">van-button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> @<span class="attr">click</span>=<span class="string">&quot;toCam(&#x27;album&#x27;)&quot;</span>&gt;</span>上传照片认证<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span> <span class="attr">id</span>=<span class="string">&quot;input&quot;</span> <span class="attr">accept</span>=<span class="string">&quot;image/*&quot;</span> @<span class="attr">change</span>=<span class="string">&quot;fileChange&quot;</span></span></span><br><span class="line"><span class="tag"><span class="attr">ref</span>=<span class="string">&quot;avatarCamera&quot;</span> <span class="attr">style</span>=<span class="string">&quot;display: none&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">toCam(type) &#123;</span><br><span class="line">    let cameraInput = this.$refs[&#x27;avatarCamera&#x27;];</span><br><span class="line">    if (type === &#x27;camera&#x27;) &#123;</span><br><span class="line">      // 调起前置摄像头</span><br><span class="line">      cameraInput.setAttribute(&#x27;capture&#x27;, &#x27;user&#x27;);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      cameraInput.removeAttribute(&#x27;capture&#x27;);</span><br><span class="line">    &#125;</span><br><span class="line">    cameraInput.click();</span><br><span class="line">&#125;,</span><br><span class="line">async fileChange(e) &#123;</span><br><span class="line">    let files = e.target.files;</span><br><span class="line">    if (!files || files.length === 0) return;</span><br><span class="line">    let file = files[0];</span><br><span class="line">    const res = await this.$wxUtils.compressImg(file);</span><br><span class="line">    this.imgSrc = res.afterSrc;</span><br><span class="line">    // 调用后端接口验证身份</span><br><span class="line">    this.fetchfaceDetect(file);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line">// 公众号</span><br><span class="line"><span class="tag">&lt;<span class="name">van-image</span> <span class="attr">width</span>=<span class="string">&quot;300&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span> <span class="attr">round</span> <span class="attr">fit</span>=<span class="string">&quot;cover&quot;</span> <span class="attr">:src</span>=<span class="string">&quot;imgSrc&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">van-button</span> @<span class="attr">click</span>=<span class="string">&quot;takePhoto(&#x27;camera&#x27;)&quot;</span>&gt;</span>拍照认证<span class="tag">&lt;/<span class="name">van-button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> @<span class="attr">click</span>=<span class="string">&quot;takePhoto(&#x27;album&#x27;)&quot;</span>&gt;</span>上传图片认证<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">created() &#123;</span><br><span class="line">    this.checkWxConfig();</span><br><span class="line">&#125;,</span><br><span class="line">async checkWxConfig() &#123;</span><br><span class="line">    const res = await getWxJsApiconfig();</span><br><span class="line">    const &#123; appId, timestamp, nonceStr, signature &#125; = res || &#123;&#125;;</span><br><span class="line">    window.wx.config(&#123;</span><br><span class="line">        debug: false,</span><br><span class="line">        appId,</span><br><span class="line">        timestamp,</span><br><span class="line">        nonceStr,</span><br><span class="line">        signature,</span><br><span class="line">        jsApiList: [&#x27;checkJsApi&#x27;, &#x27;chooseImage&#x27;, &#x27;getLocalImgData&#x27;],</span><br><span class="line">    &#125;);</span><br><span class="line">    window.wx.ready(() =&gt; &#123;</span><br><span class="line">        window.wx.checkJsApi(&#123;</span><br><span class="line">            jsApiList: [&#x27;checkJsApi&#x27;, &#x27;chooseImage&#x27;, &#x27;getLocalImgData&#x27;],</span><br><span class="line">            success: data =&gt; &#123;</span><br><span class="line">              console.log(&#x27;配置检查结果成功&#x27;, data);</span><br><span class="line">            &#125;,</span><br><span class="line">            fail: data =&gt; &#123;</span><br><span class="line">              console.log(&#x27;配置检查结果失败&#x27;, data);</span><br><span class="line">            &#125;,</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">    window.wx.error(err =&gt; &#123;</span><br><span class="line">        console.log(&#x27;微信配置错误~~~&#x27;, err);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;,</span><br><span class="line">takePhoto(sourceType) &#123;</span><br><span class="line">    if (!this.$wxUtils.isWeiXin()) &#123;</span><br><span class="line">        this.$toast(&#x27;请在微信公众号或微信小程序环境下使用该服务&#x27;);</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    window.wx.chooseImage(&#123;</span><br><span class="line">        count: 1, // 默认9</span><br><span class="line">        sizeType: [&#x27;original&#x27;, &#x27;compressed&#x27;],</span><br><span class="line">        sourceType: [sourceType], // &#x27;album&#x27; 相册, &#x27;camera&#x27; 相机</span><br><span class="line">        success: data =&gt; &#123;</span><br><span class="line">            window.wx.getLocalImgData(&#123;</span><br><span class="line">                localId: data.localIds[0],</span><br><span class="line">                success: res =&gt; &#123;</span><br><span class="line">                    let localData = res.localData;</span><br><span class="line">                    // 兼容处理：安卓获取的图片 base64 码没有前缀，而苹果有</span><br><span class="line">                    if (localData.indexOf(&#x27;data:image&#x27;) === -1) &#123;</span><br><span class="line">                        localData = &#x27;data:image/png;base64,&#x27; + localData;</span><br><span class="line">                    &#125;</span><br><span class="line">                    localData = localData.replace(/\r|\n/g, &#x27;&#x27;).replace</span><br><span class="line">                    (&#x27;data:image/jgp&#x27;, &#x27;data:image/jpeg&#x27;);</span><br><span class="line">                    this.imgSrc = localData;</span><br><span class="line">                    this.fetchfaceDetect();</span><br><span class="line">                &#125;,</span><br><span class="line">                fail: error =&gt; &#123;</span><br><span class="line">                    console.log(&#x27;转换图片为 base64 失败&#x27;, error);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;,</span><br><span class="line">        fail: error =&gt; &#123;</span><br><span class="line">            console.log(&#x27;调起相机失败&#x27;, error);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;,</span><br><span class="line">fetchfaceDetect(file) &#123;</span><br><span class="line">    const toast = this.$toast.loading(&#123;</span><br><span class="line">        mask: true,</span><br><span class="line">        duration: 0,</span><br><span class="line">        loadingType: &#x27;spinner&#x27;,</span><br><span class="line">        message: &#x27;识别中...&#x27;,</span><br><span class="line">    &#125;);</span><br><span class="line">    const &#123; name, idCard &#125; = this.$route.query;</span><br><span class="line">    const url = &#x27;/ehCard/healthCard/faceDetect&#x27;;</span><br><span class="line">    let params = new FormData();</span><br><span class="line">    params.append(&#x27;file&#x27;, file);</span><br><span class="line">    params.append(&#x27;name&#x27;, name);</span><br><span class="line">    params.append(&#x27;idCard&#x27;, idCard);</span><br><span class="line"></span><br><span class="line">    this.$postRequest(url, params, false, &#123;</span><br><span class="line">        &#x27;Content-Type&#x27;: &#x27;multipart/form-data&#x27;,</span><br><span class="line">        showError: false,</span><br><span class="line">    &#125;)</span><br><span class="line">    .then((&#123; data &#125;) =&gt; &#123;</span><br><span class="line">        toast.clear();</span><br><span class="line">        if (data.success) &#123;  &#125; </span><br><span class="line">    &#125;)</span><br><span class="line">    .catch(() =&gt; &#123;  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="小程序人脸"><a href="#小程序人脸" class="headerlink" title="小程序人脸"></a>小程序人脸</h2><blockquote>
<p>由于功能涉及到用户的隐私信息，因此调用人脸核身接口的小程序需要在<a target="_blank" rel="external nofollow noopener noreferrer" href="https://mp.weixin.qq.com/">微信公众平台-功能-人脸核身</a>申请权限并满足<a target="_blank" rel="external nofollow noopener noreferrer" href="https://developers.weixin.qq.com/community/business/doc/000442d352c1202bd498ecb105c00d">一定条件</a>。微信会在触发接口后跳转到人脸识别界面，然后将用户人脸信息与人脸库对比后返回认证信息。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、检测设备是否支持</span></span><br><span class="line"><span class="title function_">checkIsSupportFacialRecognition</span>(&#123;</span><br><span class="line">    <span class="attr">checkAliveType</span>: <span class="number">2</span>,</span><br><span class="line">　　 <span class="attr">success</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123;</span><br><span class="line">  　　   <span class="keyword">if</span> (res.<span class="property">errCode</span> == <span class="number">0</span> || </span><br><span class="line">        res.<span class="property">errMsg</span> == <span class="string">&quot;checkIsSupportFacialRecognition:ok&quot;</span>) &#123;</span><br><span class="line">            <span class="comment">// 2、调用人脸识别</span></span><br><span class="line">          　wx.<span class="title function_">startFacialRecognitionVerify</span>(&#123;</span><br><span class="line">                <span class="attr">name</span>: <span class="string">&quot;姓名&quot;</span>,</span><br><span class="line">                <span class="attr">idCardNumber</span>: <span class="string">&quot;身份证号&quot;</span>,</span><br><span class="line">                success (res) &#123;</span><br><span class="line">                    <span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="property">verifyResult</span>)</span><br><span class="line">                &#125;,</span><br><span class="line">                <span class="attr">fail</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">                    wx.<span class="title function_">showModal</span>(&#123;</span><br><span class="line">                        <span class="attr">title</span>: <span class="string">&#x27;提示&#x27;</span>,</span><br><span class="line">                        <span class="attr">showCancel</span>: <span class="literal">false</span>,</span><br><span class="line">                        <span class="attr">content</span>: <span class="string">&quot;人脸识别失败！&quot;</span> + res.<span class="property">errMsg</span>,</span><br><span class="line">                        <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">                            wx.<span class="title function_">navigateTo</span>()</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">　　　　 &#125;</span><br><span class="line">        wx.<span class="title function_">showToast</span>(<span class="string">&#x27;微信版本过低，暂时无法使用此功能，请升级最新版本&#x27;</span>)</span><br><span class="line">　  &#125;,</span><br><span class="line">　　<span class="attr">fail</span>: <span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">　　　　wx.<span class="title function_">showToast</span>(<span class="string">&#x27;微信版本过低，暂时无法使用此功能，请升级最新版本&#x27;</span>)</span><br><span class="line">　　&#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="小程序语音"><a href="#小程序语音" class="headerlink" title="小程序语音"></a>小程序语音</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// index.wxml</span></span><br><span class="line">&lt;button type=<span class="string">&#x27;default&#x27;</span> bindtouchstart=<span class="string">&#x27;startRecord&#x27;</span> </span><br><span class="line">bindtouchend=<span class="string">&quot;StopRecord&quot;</span>&gt;录音&lt;/button&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">// index.js</span></span><br><span class="line"><span class="keyword">const</span> recorderManager = wx.<span class="title function_">getRecorderManager</span>();</span><br><span class="line">recorder.<span class="title function_">onStop</span>(<span class="function"><span class="params">result</span> =&gt;</span> &#123;</span><br><span class="line">    wx.<span class="title function_">getFileSystemManager</span>().<span class="title function_">readFile</span>(&#123;</span><br><span class="line">        <span class="attr">filePath</span>: result.<span class="property">tempFilePath</span>,</span><br><span class="line">        <span class="title function_">success</span>(<span class="params">res</span>) &#123;</span><br><span class="line">            wx.<span class="title function_">request</span>(&#123;</span><br><span class="line">                <span class="attr">url</span>: <span class="string">&#x27;https://vop.baidu.com/server_api&#x27;</span>,</span><br><span class="line">                <span class="attr">data</span>: &#123;</span><br><span class="line">                    <span class="attr">format</span>: <span class="string">&#x27;pcm&#x27;</span>,</span><br><span class="line">                    <span class="attr">rate</span>: <span class="number">16000</span>,</span><br><span class="line">                    <span class="attr">channel</span>: <span class="number">1</span>,</span><br><span class="line">                    <span class="attr">cuid</span>: <span class="string">&#x27;sdfdfdfsfs&#x27;</span>,</span><br><span class="line">                    <span class="attr">token</span>: wx.<span class="title function_">getStorageSync</span>(<span class="string">&quot;token&quot;</span>),</span><br><span class="line">                    <span class="attr">speech</span>: wx.<span class="title function_">arrayBufferToBase64</span>(res.<span class="property">data</span>),</span><br><span class="line">                    <span class="attr">len</span>: res.<span class="property">data</span>.<span class="property">byteLength</span></span><br><span class="line">                &#125;,</span><br><span class="line">                <span class="attr">method</span>: <span class="string">&#x27;POST&#x27;</span>,</span><br><span class="line">                <span class="attr">header</span>: &#123; <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span> &#125;,</span><br><span class="line">                <span class="title function_">success</span>(<span class="params">res</span>) &#123; &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="title class_">Page</span>(&#123;</span><br><span class="line">    startRecord () &#123;</span><br><span class="line">        wx.<span class="title function_">showLoading</span>(&#123; <span class="attr">title</span>: <span class="string">&#x27;正在录音中...&#x27;</span> &#125;)</span><br><span class="line">        recorder.<span class="title function_">start</span>(&#123;</span><br><span class="line">            <span class="attr">format</span>: <span class="string">&#x27;PCM&#x27;</span>,</span><br><span class="line">            <span class="attr">duration</span>: <span class="number">5000</span>,</span><br><span class="line">            <span class="attr">sampleRate</span>: <span class="number">16000</span>,</span><br><span class="line">            <span class="attr">numberOfChannels</span>: <span class="number">1</span>,</span><br><span class="line">            <span class="attr">encodeBitRate</span>: <span class="number">48000</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line">    stopRecord () &#123;</span><br><span class="line">        recorder.<span class="title function_">stop</span>();</span><br><span class="line">        wx.<span class="title function_">hideLoading</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>




    </div>

    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;margin-top:20px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chuangges
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chuangges.gitee.io/2023/01/10/19-%E7%99%BE%E5%BA%A6%E8%AF%86%E5%88%AB/" title="百度智能云之在线识别">https://chuangges.gitee.io/2023/01/10/19-百度识别/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh_CN" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/11/10/18-%E5%BE%AE%E6%9C%8D%E5%8A%A1/" rel="prev" title="微服务技术栈">
      <i class="fa fa-chevron-left"></i> 微服务技术栈
    </a></div>
      <div class="post-nav-item">
    <a href="/2023/01/17/20-%E8%B7%A8%E5%B9%B3%E5%8F%B0%E6%96%87%E6%A1%A3/" rel="next" title="文档的在线预览和上传下载">
      文档的在线预览和上传下载 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="nav-number">1.</span> <span class="nav-text">一、准备工作</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%8E%A5%E5%8F%A3%E5%B0%81%E8%A3%85"><span class="nav-number">2.</span> <span class="nav-text">二、接口封装</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81H5-%E8%AF%86%E5%88%AB"><span class="nav-number">3.</span> <span class="nav-text">三、H5 识别</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0"><span class="nav-number">3.1.</span> <span class="nav-text">图片上传</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%BA%E8%84%B8%E6%8B%8D%E7%85%A7"><span class="nav-number">3.2.</span> <span class="nav-text">人脸拍照</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%AD%E9%9F%B3%E5%BD%95%E5%88%B6"><span class="nav-number">3.3.</span> <span class="nav-text">语音录制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%AD%E9%9F%B3%E5%A4%84%E7%90%86"><span class="nav-number">3.4.</span> <span class="nav-text">语音处理</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E5%BE%AE%E4%BF%A1%E8%AF%86%E5%88%AB"><span class="nav-number">4.</span> <span class="nav-text">四、微信识别</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%8D%E7%85%A7%E4%B8%8A%E4%BC%A0"><span class="nav-number">4.1.</span> <span class="nav-text">拍照上传</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BA%BA%E8%84%B8"><span class="nav-number">4.2.</span> <span class="nav-text">小程序人脸</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E8%AF%AD%E9%9F%B3"><span class="nav-number">4.3.</span> <span class="nav-text">小程序语音</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chuangges" src="" data-original="/images/chuang.png">
  <p class="site-author-name" itemprop="name">chuangges</p>
  <div class="site-description" itemprop="description">chuangge's blog.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chuangges" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chuangges" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://juejin.im/" title="https:&#x2F;&#x2F;juejin.im&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">掘金</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.nowcoder.com/" title="https:&#x2F;&#x2F;www.nowcoder.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">牛客网</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.docschina.org/" title="https:&#x2F;&#x2F;www.docschina.org&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">印记中文</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://tool.lu/" title="http:&#x2F;&#x2F;tool.lu&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">在线工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tinypng.com/" title="https:&#x2F;&#x2F;tinypng.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片压缩</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://bigjpg.com/" title="https:&#x2F;&#x2F;bigjpg.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片放大</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tool.chinaz.com/" title="https:&#x2F;&#x2F;tool.chinaz.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">站长工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://learn-anything.xyz/" title="https:&#x2F;&#x2F;learn-anything.xyz&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Learn-Anything</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Stack-Overflow</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chuangge</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">1m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">15:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(e){e.imageLazyLoadSetting.processImages=t;var n=e.imageLazyLoadSetting.isSPA,i=e.imageLazyLoadSetting.preloadRatio||1,r=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){n&&(r=o());for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(e.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];e=function(){r=r.filter(function(t){return o!==t})},(t=o).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,t.removeAttribute("data-original"),e&&e()},t.src!==i&&(n.src=i))}()}function a(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",a),e.addEventListener("resize",a),e.addEventListener("orientationchange",a)}(this);</script></body>
</html>
